package mips.instructions;

/**
 * <code>XORI</code> instruction<br/>
 * Exclusive OR Immediate<br/>
 * @author jnmartin84@gmail.com
 */
public class XORI extends Instruction {

	private static final String INSTRUCTION_NAME = "XORI";
	private static final XORI INSTANCE = new XORI();

	private XORI(){}

	public static final XORI getInstance() {
		return INSTANCE;
	}

	/**
	 * <b>Format:</b><br/>
	 * XORI rt, rs, immediate<br/><br/>
	 * <b>Description:</b><br/>
	 * The 16-bit immediate is zero-extended and combined with the contents of<br/>
	 * general register rs in a bit-wise logical exclusive OR operation.<br/>
	 * The result is placed into general register rt.<br/><br/>
	 * <b>Operation:</b><br/>
	 * T: GPR[rt] &larr; GPR[rs] xor (0<sup>16</sup> || immediate)
	 */
	@Override
	public final void execute(final int instruction) {

		mips.instructions.Instruction.RS = (instruction >> 21) & 0x0000001F;
		mips.instructions.Instruction.RT = (instruction >> 16) & 0x0000001F;
		mips.instructions.Instruction.IMMED = (instruction) & 0x0000FFFF;

		mips.R4300i.GPR[mips.instructions.Instruction.RT] = mips.R4300i.GPR[mips.instructions.Instruction.RS] ^ mips.instructions.Instruction.IMMED;

		mips.R4300i.PC = mips.R4300i.nPC;
		mips.R4300i.nPC = mips.R4300i.PC + 4;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public final String emit(final int instruction) {

		mips.instructions.Instruction.RS = (instruction >> 21) & 0x0000001F;
		mips.instructions.Instruction.RT = (instruction >> 16) & 0x0000001F;
		mips.instructions.Instruction.IMMED = (instruction) & 0x0000FFFF;

		return 	"		mips.CPU.GPR["+mips.instructions.Instruction.RT+"] = mips.CPU.GPR["+mips.instructions.Instruction.RS+"] ^ "+mips.instructions.Instruction.IMMED+";\n" +
				"		\n" +
				"		mips.CPU.PC = mips.CPU.nPC;\n" +
				"		mips.CPU.nPC = mips.CPU.PC + 4;\n";
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public final String getName(final int instruction) {
		return getName();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public final String getName() {
		return INSTRUCTION_NAME;
	}
}